﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using BusinessBingo.Queries;
using BusinessBingo.QueryHandlers.Base;
using BusinessBingo.QueryHandlers.Interfaces;
using BusinessBingo.QueryHandlers.Results;
using BusinessBingo.Model;
using Dapper;

namespace BusinessBingo.QueryHandlers
{
    public class GetWordsQueryHandler : BaseQueryHandler, IGetWordsQueryHandler
    {

        public GetWordsQueryHandlerResult Execute(GetWordsQuery query)
        {
            GetWordsQueryHandlerResult result = new GetWordsQueryHandlerResult();
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BusinessBingoDB"].ConnectionString))
            {
                conn.Open();
                var words = conn.Query<Word>(@"SELECT  *
                                               FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY Id ) AS RowNum, *
                                               FROM      [BusinessBingo].[dbo].[Word]
                                               WHERE LCID = '1031') AS RowConstrainedResult
                                               WHERE   RowNum >= " + query.Paging.RowFrom + @"
                                               AND RowNum < " + query.Paging.RowTo + @"
                                               ORDER BY RowNum");
                result.Words = words;
            }

            return result;
        }

    }
}